import redis
class Redis:
    def __init__(self):
        self.conn = redis.Redis(host='localhost', port=6379, db=1)

    # 存
    def set_str(self, mobile, codes, time=0):
        if time == 0:
            self.conn.set(mobile, codes)
        else:
            self.conn.setex(mobile, time, codes)

    # 取
    def get_str(self, mobile):
        value = self.conn.get(mobile)
        if value:
            value = str(value, encoding='utf8')
        return value

    # 删
    def del_str(self, mobile):
        self.conn.delete(mobile)

    # 存集合
    def insert_set(self, key, val):
        self.conn.sadd(key, *val)

    # 取集合
    def find_set(self, key):
        val = self.conn.smembers(key)
        if val:
            list = []
            for i in val:
                list.append(str(i, encoding='utf8'))
            return list
        else:
            return None

    #hash类型添加
    def insert_hash(self,key,params,values):
        self.conn.hset(key,params,values)

    #获取某个属性的值
    def get_value_hash(self,key,parmas):
        return self.conn.hget(key,parmas)

    #获取所有属性的值
    def get_all_hash(self,key):
        return self.conn.hgetall(key)

    #删除某个属性
    def del_hash(self,key,parmas):
        self.conn.hdel(key,parmas)



r = Redis()

# r.insert_set('xx1',['/index', '/user'])
# qu = r.find_set('xx1')
# print(qu)
